﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mindscape.LightSpeed;
using OrmSmackdown.LightSpeedORM.Mappings;
using System.IO;

namespace OrmSmackdown.LightSpeedORM
{
    public class Exerciser : IExerciser
    {
        private LightSpeedContext<Mappings.OrmSmackdownUnitOfWork> ctx;
        private Mappings.OrmSmackdownUnitOfWork uow;

        public Exerciser()
        {
            ctx = new LightSpeedContext<OrmSmackdownUnitOfWork>();
            ctx.ConnectionString = LightSpeedContext.Default.ConnectionString;
            ctx.PluralizeTableNames = LightSpeedContext.Default.PluralizeTableNames;
            ctx.IdentityMethod = LightSpeedContext.Default.IdentityMethod;

            uow = ctx.CreateUnitOfWork();
        }

        #region Insert
        public void Insert()
        {
        
                var managerRole = new Role { RoleName = Constants.ManagerRole, IsForContact = false, IsForPortal = true };
                var employeeRole = new Role { RoleName = Constants.EmployeeRole, IsForContact = false, IsForPortal = true };
                var contactRole = new Role { RoleName = Constants.ContactRole, IsForContact = true, IsForPortal = false };

                var user1 = new User { FirstName = Constants.User1FirstName, LastName = Constants.User1LastName, Email = Constants.User1Email, Role = managerRole };
                var user2 = new User { FirstName = Constants.User2FirstName, LastName = Constants.User2LastName, Email = Constants.User2Email, Role = employeeRole };
                var user3 = new User { FirstName = Constants.User3FirstName, LastName = Constants.User3LastName, Email = Constants.User3Email, Role = employeeRole };
                var user4 = new User { FirstName = Constants.User4FirstName, LastName = Constants.User4LastName, Email = Constants.User4Email, Role = managerRole };
                var user5 = new User { FirstName = Constants.User5FirstName, LastName = Constants.User5LastName, Email = Constants.User5Email, Role = employeeRole };

                var company1 = new Company { CompanyName = Constants.Company1 };
                var company2 = new Company { CompanyName = Constants.Company2 };

                var contact1 = new Contact { FirstName = Constants.Contact1FirstName, LastName = Constants.Contact1LastName, Email = Constants.Contact1Email, Role = contactRole, Company = company1 };
                var contact2 = new Contact { FirstName = Constants.Contact2FirstName, LastName = Constants.Contact2LastName, Email = Constants.Contact2Email, Role = contactRole, Company = company1 };
                var contact3 = new Contact { FirstName = Constants.Contact3FirstName, LastName = Constants.Contact3LastName, Email = Constants.Contact3Email, Role = contactRole, Company = company2 };
                var contact4 = new Contact { FirstName = Constants.Contact4FirstName, LastName = Constants.Contact4LastName, Email = Constants.Contact4Email, Role = contactRole, Company = company2 };
                var contact5 = new Contact { FirstName = Constants.Contact5FirstName, LastName = Constants.Contact5LastName, Email = Constants.Contact5Email, Role = contactRole, Company = company2 };
                var contact6 = new Contact { FirstName = Constants.Contact6FirstName, LastName = Constants.Contact6LastName, Email = Constants.Contact6Email, Role = contactRole, Company = company2 };

                var portal1 = new Portal { PortalName = Constants.Portal1, AdminUser = user1 };
                var portal2 = new Portal { PortalName = Constants.Portal2, AdminUser = user4 };

                portal1.PeopleInPortal.Add(user1);

                portal1.PeopleInPortal.Add(user2);
                portal2.PeopleInPortal.Add(user2);
            

                portal1.PeopleInPortal.Add(user3);
                portal2.PeopleInPortal.Add(user3);

                portal2.PeopleInPortal.Add(user4);
                portal2.PeopleInPortal.Add(user5);

                using (var uow = ctx.CreateUnitOfWork())
                {
                    uow.Add(company1);
                    uow.Add(company2);

                    uow.Add(managerRole);
                    uow.Add(employeeRole);
                    uow.Add(contactRole);

                    uow.Add(user1);
                    uow.Add(user2); 
                    uow.Add(user3);
                    uow.Add(user4);
                    uow.Add(user5);

                    uow.Add(contact1);
                    uow.Add(contact2);
                    uow.Add(contact3);
                    uow.Add(contact4);
                    uow.Add(contact5);
                    uow.Add(contact6);

                    AddAddress(uow, user1, "1000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, user2, "2000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, user3, "3000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, user4, "4000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, user5, "5000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, user1, "1100 Some St.", string.Empty, Constants.TownWeCareAbout, "ST", "12345", "1234", true);
                    AddAddress(uow, user2, "1200 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, user3, "1300 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, user4, "1400 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, user5, "1500 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);

                    AddAddress(uow, contact1, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact2, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact3, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact4, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact5, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact6, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                    AddAddress(uow, contact1, "8000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, contact2, "8100 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, contact3, "8200 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, contact4, "8300 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, contact5, "8400 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                    AddAddress(uow, contact6, "8500 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);

                    uow.SaveChanges();
                }
        }

        private void AddAddress(OrmSmackdownUnitOfWork uow, Person p, string line1, string line2, string city, string state, string zip, string zip4, bool isDefault)
        {
            var addr = new Address { Line1 = line1, Line2 = line2, City = city, State = state, ZipCode = zip, ZipCodePlusFour = zip4, IsDefault = isDefault, Person = p };         
        }
        #endregion

        public IList<string> GetUserEmails()
        {
            return (from p in uow.Users
                   select p.Email).ToList();
        }

        public IList<string> GetContactEmails()
        {
            return (from p in uow.Contacts
                    select p.Email).ToList();
        }

        public IList<string> GetPortalAdminEmails()
        {
            return (from portals in uow.Portals select portals.AdminUser.Email).ToList();
        }

        public IList<string> GetContactEmailsForCompany(string companyName)
        {
            return uow.Companies.Where(cmp => cmp.CompanyName == companyName).FirstOrDefault().People.Select(fld => fld.Email).ToList();
        }

        public int GetNumberOfPortals()
        {
            return uow.Portals.Count();
        }

        public int GetNumberOfUsers()
        {
            return uow.Users.Count();
        }

        public int GetNumberOfContacts()
        {
            return uow.Contacts.Count();
        }

        public string GetDefaultCity(string email)
        {
            var user = uow.People.Where(p => p.Email == email).FirstOrDefault();

            return user.Addresses.Where(a => a.IsDefault == true).FirstOrDefault().City;
        }

        public string GetAdminForPortal(string portalName)
        {
            var portal = uow.Portals.Where(prt => prt.PortalName == portalName).FirstOrDefault();
            
            return portal.AdminUser.Email;
        }

        public double GetAverageUsersPerPortal()
        {
            //Average is not implemented.
            //return uow.Portals.Average(prt => prt.PortalPeopleXrefs.Count);

            var portals = uow.Portals.ToList();

            ///Ehhh... on this one, fairly certain there is a better way, runs three
            ///queries, yuk!
            int numbUsers = 0;
            foreach (var portal in portals)
                numbUsers += ((Portal)portal).PeopleInPortal.Count;

            return (double)numbUsers / (double)portals.Count;
        }

        public void Dispose()
        {
            uow.Dispose();
        }
    }
}
